﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 学生选课及成绩查询管理系统
{
    public partial class addStudentInfo : Form
    {
        public addStudentInfo()
        {
            InitializeComponent();
        }

        private void addStudentInfo_Load(object sender, EventArgs e)
        {
            string sql = "select * from student ";
            DataTable dt=DBAccess.GetTable(sql);
            dataGridView1.DataSource = dt;
            
        }

        private void btn_addStu_Click(object sender, EventArgs e)
        {
            string stuName = txt_stuName.Text;
            string stuSex = txt_stuSex.Text;
            string stuAge = txt_stuAge.Text;
            string stuClaNo = txt_stuClaNo.Text;
            if (string.IsNullOrEmpty(stuName) || string.IsNullOrEmpty(stuSex) || string.IsNullOrEmpty(stuAge) || string.IsNullOrEmpty(stuClaNo))
            {
                MessageBox.Show("请输入完整信息");
                return;
            }
            int flag = -1;
            if (int.TryParse(stuSex, out flag))
            {
                if (flag == 0 || flag == 1)
                {

                }
                else
                {
                    MessageBox.Show("请输入0或1代表性别！");
                    return;
                }
            }
            else
            {
                MessageBox.Show("请输入0或1代表性别！");
                return;
            }

            if (int.TryParse(stuAge,out flag))
            {
                if (flag<0||flag>150)
                {
                    MessageBox.Show("请输入0-150之间的整型数值！");
                    return;
                }
            }
            else
            {
                MessageBox.Show("请输入0-150之间的整型数值！");
                return;
            }
            string sql = "insert into student(stuname,stugender,stuage,classno) values(@name,@sex,@age,@classNO)";
            SqlParameter[] sqlarray = { new SqlParameter("@name",stuName),new SqlParameter("@sex",stuSex),
                                          new SqlParameter("@age",stuAge),new SqlParameter("@classNO",stuClaNo)};

            int res =DBAccess.GetState(sql,sqlarray);
            if(res!=0){
                MessageBox.Show("成功添加学生信息");
                addStudentInfo_Load(sender,e);
            }
            else
            {
                MessageBox.Show("添加失败");
            }
           


        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int row = dataGridView1.CurrentCellAddress.Y + 1;
            int col = dataGridView1.CurrentCellAddress.X + 1;
            txt_stuName.Text = dataGridView1.Rows[row - 1].Cells[1].Value.ToString();
            txt_stuSex.Text = dataGridView1.Rows[row - 1].Cells[2].Value.ToString();
            txt_stuAge.Text = dataGridView1.Rows[row - 1].Cells[3].Value.ToString();
            txt_stuClaNo.Text = dataGridView1.Rows[row - 1].Cells[4].Value.ToString();
        }
    }
}
